#include "userlist.h"

UserList::UserList()
{
    //读取本地数据
    vector <User*>().swap(*UserList::userlist);
    //体验账户
    User *u_one = new User("abc", "123456", "0", "0");
    UserList::userlist->push_back(u_one);
    ifstream ifs("userlist.txt", ios::in);
    if (ifs)
    {
        string line;
        int a = 1;
        string name;
        string password;
        string number;
        string winNumber;
        string s;
        while (ifs >> s) {
                if (a%4 == 1) {
                    name = s;
                }
                else if (a%4 == 2) {
                    password = s;

                }
                else if (a%4 == 3) {
                    number = s;
                }
                else{
                    winNumber = s;
                    User *u = new User(name, password, number, winNumber);
                    UserList::userlist->push_back(u);
                }
                ++a;
            }
    }
    ifs.close();
}

UserList::~UserList()
{
    save();
}

void UserList::save()
{
    //保存数据
    ofstream ofs("userlist.txt", ios::out);
    // 体验账号不保存数据，所以从1开始
    for (int i = 1;i < UserList::userlist->size(); i++)
    {
        string data ="";
        User *u = UserList::userlist->at(i);
        data = u->name + " " + u->password + " " + u->number + " " + u->winNumber;
        ofs << data << endl;
    }
    ofs.close();
}
